//
//  ContentView.swift
//  TestActorDownload
//
//  Created by rolodestar on 2025/6/13.
//

import SwiftUI
import SwiftData



struct ContentView: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var items: [DownloadDataModel]
    @EnvironmentObject var  manager: DownloadManager

    var body: some View {
        NavigationSplitView {
            List {
                ForEach(items) { item in
                    NavigationLink {
                        DetailView(item: item)
                    } label: {
                        VStack{
                            Text("\(item.status)")
                                .foregroundStyle(item.localStoredFilePath != nil ? .blue : .white)
                            if  item.currentProcess > 0 && item.currentProcess < 1{
                                ProgressView(value: item.currentProcess, total: 1)
                                    .progressViewStyle(.linear)
                                    .frame(height: 20)
                            }
                        }
                    }
                }
                .onDelete(perform: deleteItems)
            }
#if os(macOS)
            .navigationSplitViewColumnWidth(min: 180, ideal: 200)
#endif
            .toolbar {
#if os(iOS)
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
#endif
                ToolbarItem {
//                    Button(action: addItem) {
//                        Label("Add Item", systemImage: "plus")
//                    }
                    Button(action: gear) {
                        Label("gear", systemImage: "gear")
                    }
                }
                ToolbarItem {
//                    Button(action: addItem) {
//                        Label("Add Item", systemImage: "plus")
//                    }
                    Button(action: {
                        for item in items {
                            modelContext.delete(item)
                        }
                    }, label: {
                        Label("trash", systemImage: "trash")
                    })
                }
            }
        } detail: {
            Text("Select an item")
        }
    }

    private func addItem() {
        withAnimation {
            let newItem = Item(timestamp: Date())
            modelContext.insert(newItem)
        }
    }
    private func gear() {
//        let url = URL(string: "https://upos-szbyjkm8g1.bilivideo.com/upgcxcode/79/23/95892379/95892379-1-208.mp4?e=ig8euxZM2rNcNbNa7zdVhwdlhbuahwdVhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&trid=49db7d0cc16b426093f9a24780c6fc8u&mid=0&deadline=1749639557&gen=playurlv3&nbs=1&uipk=5&platform=pc&oi=2672518824&os=08cbv&og=hw&upsig=8ed416b26e85257f47544a71a98e5b1a&uparams=e,trid,mid,deadline,gen,nbs,uipk,platform,oi,os,og&bvc=vod&nettype=0&bw=2227509&f=u_0_0&agrr=1&buvid=&build=0&dl=0&orderid=1,3")!
        let url = URL(string: "https://upos-sz-mirrorcos.bilivideo.com/upgcxcode/79/23/95892379/95892379-1-208.mp4?e=ig8euxZM2rNcNbNa7zdVhwdlhbuahwdVhoNvNC8BqJIzNbfq9rVEuxTEnE8L5F6VnEsSTx0vkX8fqJeYTj_lta53NCM=&uipk=5&nbs=1&deadline=1749836376&gen=playurlv2&os=bcache&oi=2032490763&trid=00007da2c40623674ba497707b4f5752b4ffT&mid=3493287610551129&platform=html5&og=hw&upsig=dd806d34992222d0ca81892871f4cfa7&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform,og&cdnid=87202&bvc=vod&nettype=0&bw=278438&orderid=0,1&buvid=&build=0&mobi_app=&f=T_0_0&logo=80000000")!
//        let url = URL(string: "https://alimov2.a.kwimgs.com/upic/2023/03/30/21/BMjAyMzAzMzAyMTQ5NTZfOTQ0MjI3OThfOTkzNzc4OTA3NzZfMV8z_b_B636d5cd22a8d53502669cb018bc9ffff.mp4?clientCacheKey=3xejy4ph2dq6niu_b.mp4&tt=b&di=78e498d6&bp=13414")!
//        let shared =  DownloadManagerActor.createShared(modelContext: modelContext)
//        Task{
//            try await shared.addDownloadTask(url: url)
//        }
        
        let id = UUID()
        let model = DownloadDataModel(id: id, remoteURL: url)
        Task{
            try await manager.addDownloadTask(model: model)
        }
        modelContext.insert(model)
        
        
    }

    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            for index in offsets {
                modelContext.delete(items[index])
            }
        }
    }
}

#Preview {
    ContentView()
        .modelContainer(for: Item.self, inMemory: true)
}
